//studentBasicInfoDTO.setAge(studentBasicInfoVO.getAge());			//>????????????????????????????


// 页面加载时，自动执行
$(document).ready(function () {
    console.log("页面自动加载");

    // 联动菜单，查询省信息
    // queryProvinces();

    // let provinceId = "130000";
    // queryCities(provinceId );

    // let areaid = "110100";
    // queryAreas(areaid);

    initGanged();

});

// 修改联动反显
function initGanged() {

    // 请求的URL
    var url = $("#basePath").val() + "/linkage/provinces";
    console.log("queryProvinces-----url:::" + url);

    // ajax查询
    $.ajax({
        type: "POST", //请求类型 get , put, delete
        url: url, // 请求地址
        data: JSON.stringify({}), // data 请求参数
        dataType: "json",  // 返回数据类型
        contentType: "application/json;charset=utf-8",
        success: function (data, textStatus, jqXHR) {
            if (data.code === "0000") {
                let provinceArr = data.data;
                var sOption = "<option value=''>请选择省</option>";
                for (let i = 0; i < provinceArr.length; i++) {
                    let str = "<option value='" + provinceArr[i].provinceid + "'>" + provinceArr[i].province + "</option>";
                    sOption = sOption + str;
                }

                $("#provinces").html();
                $("#provinces").html(sOption);

                $("#provinces").find("option").each(function () {
                    // console.log("provinces---value:::" + $(this).val());

                    if ($(this).val() == $("#provincesId").text()) {
                        $(this).attr("selected", "selected");           //???????(不懂)
                        // maxStep = $(this).val();
                    }
                });

                /**
                 *
                 $("#provinces option").each(function () {
                    //遍历所有option
                    var value = $(this).val();   //获取option值
                    var text = $(this).text();
                    console.log("initGanged:value::" + value);
                    console.log("initGanged:text::" + text);
                    console.log("initGanged:aaaa::" + $("#provincesId").text());
                    if (value == $("#provincesId").text()) {
                        $(this).attr('selected', 'selected');
                    }
                });
                 */

            }
        }
    });


    $.when(queryCities($("#provincesId").text())).done(function () {
        console.log("queryCities::: whenwhenwhenwhenwhen");
        $("#provinces").find("option").each(function () {
            console.log("provinces---value:::" + $(this).val());

            if ($(this).val() == $("#citiesId").text()) {
                $(this).attr("selected", "selected");
                // maxStep = $(this).val();
            }
        });
    });


}

// 省级联动
function queryProvinces() {
    console.log("queryProvinces::::");

    // 请求的URL
    var url = $("#basePath").val() + "/linkage/provinces";
    console.log("queryProvinces-----url:::" + url);

    // ajax查询
    $.ajax({
        type: "POST", //请求类型 get , put, delete
        url: url, // 请求地址
        data: JSON.stringify({}), // data 请求参数
        dataType: "json",  // 返回数据类型
        contentType: "application/json;charset=utf-8",
        success: function (data, textStatus, jqXHR) {
            if (data.code === "0000") {
                let provinceArr = data.data;
                var sOption = "<option value=''>请选择省</option>";
                for (let i = 0; i < provinceArr.length; i++) {
                    // provinceArr[i].provinceid
                    // provinceArr[i].province

                    // <option value="你的工号">江西省</option>
                    let str = "<option value='" + provinceArr[i].provinceid + "'>" + provinceArr[i].province + "</option>";
                    sOption = sOption + str;
                }

                $("#provinces").html();
                $("#provinces").html(sOption);

            }
        }
    });
}

// 市级联动
function queryCities(provinceId) {
    console.log("queryCities-provinceIds:::" + provinceId);

    // 请求的URL
    var url = $("#basePath").val() + "/linkage/cities";
    console.log("queryCities-----url:::" + url);

    // ajax查询
    $.ajax({
        type: "POST", //请求类型 get , put, delete
        url: url, // 请求地址
        data: JSON.stringify({ "provinceId": provinceId }), // data 请求参数
        dataType: "json",  // 返回数据类型
        contentType: "application/json;charset=utf-8",
        success: function (data, textStatus, jqXHR) {
            if (data.code === "0000") {
                let cityArr = data.data;
                var sOption = "<option value=''>请选择市</option>";
                for (let i = 0; i < cityArr.length; i++) {
                    let str = "<option value='" + cityArr[i].cityid + "'>" + cityArr[i].city + "</option>";
                    sOption = sOption + str;
                }

                $("#cities").html();
                $("#cities").html(sOption);
                $("#areas").html();
                $("#areas").html("<option value=''>请选择县/区</option>");

            }
        }
    });
}


// 区县级联动
function queryAreas(cityid) {
    console.log("queryAreas-cityid:::" + cityid);

    // 请求的URL
    var url = $("#basePath").val() + "/linkage/areas";

    // ajax查询
    $.ajax({
        type: "POST", //请求类型 get , put, delete
        url: url, // 请求地址
        data: JSON.stringify({ "cityid": cityid }), // data 请求参数
        dataType: "json",  // 返回数据类型
        contentType: "application/json;charset=utf-8",
        success: function (data, textStatus, jqXHR) {
            if (data.code === "0000") {
                let areasArr = data.data;
                var sOption = "<option value=''>请选择县/区</option>";
                for (let i = 0; i < areasArr.length; i++) {
                    let str = "<option value='" + areasArr[i].areaid + "'>" + areasArr[i].area + "</option>";
                    sOption = sOption + str;
                }

                $("#areas").html();
                $("#areas").html(sOption);

            }
        }
    });
}


/**
 * 省级事件
 */
$("#provinces").click(function () {
    let provinceId = $("#provinces option:selected").val();
    queryCities(provinceId);
    // console.log("arr::::::::" + $("#provinces option")[0].val());
    // console.log("arr::length::::::" + $("#provinces option").val().length);

});

/**
 * 市级事件
 */
$("#cities").click(function () {
    let citieId = $("#cities option:selected").val();
    queryAreas(citieId);
});